package com.gzy.studentsociety.dao;

import com.gzy.studentsociety.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;


public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    // 通过学号和密码来判断是否存在该学生
    public Student login(String acc, String pwd) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "SELECT * FROM student WHERE studentNumber = ? AND password = ?";
        Student student = qr.query(sql, new BeanHandler<Student>(Student.class), acc, pwd);
        return student;
    }

    // 通过学号 查询学生信息
    public Student findByNumber(String number) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentNumber = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),number);
    }

    // 通过id 查询学生信息
    public Student findById(Integer id) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentId = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),id);
    }


    public static void main(String[] args) {
        StudentDao dao = new StudentDaoImpl();
        try {
            Student student = dao.login("2022034567", "123456");
            System.out.println(student);
        } catch (SQLException e) {
            e.printStackTrace(); // 捕获并打印异常信息
        }
    }
}
